#include "imagearea.h"

bool ImageArea::loadFile(const Glib::ustring& file_name)
{
	Glib::RefPtr<Gdk::Window> window = get_window();
	
	Gtk::Allocation allocation = get_allocation();
	const int width = allocation.get_width();
	const int height = allocation.get_height();
	
	// Load pixbuf
    image_ptr_ = Gdk::Pixbuf::create_from_file (file_name, width, height, false);
    
    // Detect transparent colors for loaded image
    Cairo::Format format = Cairo::FORMAT_RGB24;
    if (image_ptr_->get_has_alpha())
    {
        format = Cairo::FORMAT_ARGB32;
    }
    
    // Create a new ImageSurface
    image_surface_ptr_ = Cairo::ImageSurface::create  (format, image_ptr_->get_width(), image_ptr_->get_height());
    
    // Create the new Context for the ImageSurface
    image_context_ptr_ = Cairo::Context::create (image_surface_ptr_);
    
    // Draw the image on the new Context
    Gdk::Cairo::set_source_pixbuf (image_context_ptr_, image_ptr_, 0.0, 0.0);
    image_context_ptr_->paint();
    
    filename.assign(file_name);
    
    loaded_ = true;
    
    return true;
}

bool ImageArea::on_expose_event(GdkEventExpose* event)
{ 
	if (!loaded_){
		return true;
	}
	
	redraw();
    
    return true;
}

void ImageArea :: redraw(){
	// Create the context for the widget
	Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
	    
	/*
	// Select the clipping rectangle
	context->rectangle(event->area.x, event->area.y, event->area.width, event->area.height);
	    	
	context->clip();
	*/
	// Store context
	context->save();
	    
	// Draw the source image on the widget context
	context->set_source (image_surface_ptr_, 0.0, 0.0);
	context->rectangle (0.0, 0.0, image_ptr_->get_width(), image_ptr_->get_height());
	context->clip();
	context->paint();
	   
	dibujarGraficos();
	    
	// Restore context
	context->restore();
}

void ImageArea :: dibujarGraficos(){ 
	
	int ini[2], fin[2];
	
	for(int i = 0; i < vContinentes.get_Size(); i++){
		Continente *c = vContinentes.get_Continente(i);
		for(int j = 0; j < c->get_CantPaises(); j++){
			Pais *p = c->get_Pais(j);
			string nombre = p->get_Nombre();
			p->get_Pos(ini[0], ini[1]);
			for(int k = 0; k < p->cantidad_Conecciones(); k++){
				string np2, nc2;
				p->get_Coneccion(k, np2, nc2);
				Continente *c2 = vContinentes.get_Continente(nc2);
				Pais *p2 = c2->get_Pais(np2);
				p2->get_Pos(fin[0], fin[1]);
				this->dibujarRecta(ini, fin);
			}
			dibujarCirculo(ini[0], ini[1], p->get_Radio(), nombre);
		}
	}
}

void ImageArea :: cargarRuta(string ruta){
	
	filename.assign(ruta);
	loaded_ = false;

}

void ImageArea :: guardarImagen(string ruta){
	
	size_t found = filename.find_last_of(".");
	formato = filename.substr(found+1);
	
	image_ptr_->save(ruta, formato);
}

void ImageArea :: dibujarRecta(int ini[2], int fin[2]){
	
	// This is where we draw on the window
	Glib::RefPtr<Gdk::Window> window = get_window();
	if(window){
	    Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
	    cr->set_line_width(2.0);
	    // draw red lines out from the center of the window
	    cr->set_source_rgb(0.8, 0.0, 0.0);
	    cr->move_to(ini[0], ini[1]);
	    cr->line_to(fin[0], fin[1]);
	    cr->stroke();
	}
}

void ImageArea :: dibujarCirculo(int x, int y, int radio, string nombre){
	Glib::RefPtr<Gdk::Window> window = get_window();
		if(window){
		    //Dibujo el circulo
			Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
		    cr->save();
		    cr->set_line_width(10.0);
		    cr->arc(x, y, radio, 0.0, 2.0 * M_PI);
		    cr->set_source_rgba(0.5, 0.5, 0.5, 0.0);
		    cr->fill_preserve();
		    cr->restore();
		    cr->stroke();
		    
		    Glib::RefPtr<Pango::Layout> pangoLayout = Pango::Layout::create (cr);
		    cr->move_to(x-radio,y+radio+5);
		    pangoLayout->set_text(nombre);
		    pangoLayout->update_from_cairo_context(cr);  //gets cairo cursor position
		    pangoLayout->add_to_cairo_context(cr);       //adds text to cairos stack of stuff to be drawn
		    cr->stroke();       //tells Cairo to render it's stack
		}
}

